/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdMeshOp2CollectBo;
import com.comac.ins.isfd.domain.bo.IsfdMeshOp2CollectConfirmBo;
import com.comac.ins.isfd.domain.bo.query.IsfdMeshOp2CollectQueryBo;
import com.comac.ins.isfd.domain.chunks.ChunksUploadResultVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshOp2CollectHandlingVo;
import com.comac.ins.isfd.domain.vo.IsfdMeshOp2CollectVo;
import com.comac.ins.isfd.service.IIsfdMeshOP2ParseService;
import com.comac.ins.isfd.service.IIsfdMeshOp2CollectService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;


/**
 * 自然网格结果库-采集
 *
 * @author hxloongs
 * @date 2025-03-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/collect/op2")
public class IsfdMeshOp2CollectController extends BaseController {


    @Autowired
    private IIsfdMeshOp2CollectService isfdOp2CollectService;

    @Autowired
    private IIsfdMeshOP2ParseService isfdMeshOP2ParseService;

    /**
     * 查询自然网格结果库-采集列表
     */
    @SaCheckPermission("isfd:op2Collect:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdMeshOp2CollectVo> list(@RequestBody IsfdMeshOp2CollectQueryBo queryBo) {
        return isfdOp2CollectService.queryPageList(queryBo);
    }

    /**
     * 获取自然网格结果库-采集详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:op2Collect:query")
    @GetMapping("/{id}")
    public R<IsfdMeshOp2CollectVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable String id) {
        return R.ok(isfdOp2CollectService.queryById(id));
    }


    /**
     * 数据处理接口
     */
    @SaCheckPermission("isfd:op2Collect:upload")
    @PostMapping("/upload/dataHandling")
    public R<IsfdMeshOp2CollectHandlingVo> dataHandling(@RequestBody List<IsfdMeshOp2CollectBo> isfdMeshOp2CollectBoList) {
        return R.ok(isfdOp2CollectService.dataHandling(isfdMeshOp2CollectBoList));
    }

    /**
     * 确认上传
     */
    @SaCheckPermission("isfd:op2Collect:upload")
    @PostMapping("/upload/confirm")
    public R<List<ChunksUploadResultVo>> confirm(@RequestBody IsfdMeshOp2CollectConfirmBo isfdMeshOp2CollectConfirmBo) {
        return R.ok(isfdOp2CollectService.confirm(isfdMeshOp2CollectConfirmBo));
    }

    /**
     * 重新上传(重新建立链接，只上传需要更新的文件)
     */
    @SaCheckPermission("isfd:op2Collect:upload")
    @PostMapping("/upload/again")
    public R<List<ChunksUploadResultVo>> reUpload(@RequestBody IsfdMeshOp2CollectConfirmBo isfdMeshOp2CollectConfirmBo) {
        return R.ok(isfdOp2CollectService.reUpload(isfdMeshOp2CollectConfirmBo));
    }


    /**
     * 自然网格结果库-文件结构化任务触发
     */
    @SaCheckPermission("isfd:mesh:struct")
    @RepeatSubmit()
    @PostMapping("/parse/createTask")
    public void createParseTask(@RequestParam("uploadId") String uploadId) {
        isfdMeshOP2ParseService.executeParse(Collections.singletonList(uploadId));
    }

    /**
     * 自然网格结果库-文件相关删除接口
     */
    @SaCheckPermission("isfd:mesh:struct")
    @RepeatSubmit()
    @PostMapping("/delete/byUploadId")
    public void deleteByUploadId(@RequestParam("uploadId") String uploadId) {
        toAjax(isfdOp2CollectService.deleteByUploadId(uploadId));
    }
}
